package com.lilosoft.business.precity.service;

import com.lilosoft.business.cga.models.SysAttachment;
import com.lilosoft.business.precity.dao.UpProAttachmentMapper;
import com.lilosoft.business.precity.models.UpProAttachment;
import com.lilosoft.business.precity.models.UpProAttachmentExample;
import com.lilosoft.business.precity.models.UpProMaterial;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 附件相关信息
 * User: 小峰
 * Date: 2019-08-28
 * Time: 10:51
 */
@Service
public class UpProAttachmentService {
    @Autowired
    private UpProAttachmentMapper upProAttachmentMapper;

    public UpProAttachment UpProAttachmentSynchronous(UpProMaterial upProMaterial, SysAttachment attachment){
        UpProAttachment model= new UpProAttachment();
        BeanUtils.copyProperties(upProMaterial,model);
        model.setRowguid(UUIDUtils.createUUId());
        model.setObjId(upProMaterial.getRowguid());
        model.setFileName(attachment.getId());
        model.setFileRealName(attachment.getFileName());
        model.setFileType(attachment.getFileType());
        model.setFileSize(attachment.getFileSize()+"");
        model.setFilePath(attachment.getFileUrl());
        model.setFileSaveTime(attachment.getCreateDate());
        model.setFileTableName("up_pro_material");
        List<UpProAttachment> upProAttachments = selectByFileName(model.getFileName());
        if (null==upProAttachments|| upProAttachments.size()<=0) {
            upProAttachmentMapper.insertSelective(model);
        }
        return model;
    }

    public List<UpProAttachment> selectByFileName(String fileName){
        UpProAttachmentExample example= new UpProAttachmentExample();
        UpProAttachmentExample.Criteria criteria = example.createCriteria();
        criteria.andFileNameEqualTo(fileName);
        criteria.andDelStateEqualTo(0);
        return upProAttachmentMapper.selectByExample(example);
    }

    /**
     * 根据cdBatch 统计数据个数（附件相关信息）
     * @param cdBatch
     * @return
     */
    public Long countByCDbatch(String cdBatch) {
        UpProAttachmentExample example= new UpProAttachmentExample();
        UpProAttachmentExample.Criteria criteria = example.createCriteria();
        criteria.andCdBatchEqualTo(cdBatch);
        return upProAttachmentMapper.countByExample(example);
    }
}
